﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initListView();
    ui->m_pbn_sort->setCheckable(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//借助qlistmodel实现对listview的初始化
void MainWindow::initListView()
{
    m_strlist<<"初始化1"<<"初始化2"<<"初始化3"
              <<"初始化4"<<"初始化5"<<"初始化6"
              <<"初始化7"<<"初始化8"<<"初始化9"<<"初始化10";

    m_modellist = new QStringListModel(this);
    m_modellist->setStringList(m_strlist);

    ui->m_listview->setModel(m_modellist);
    ui->m_checkBox_enableEdit->setChecked(true);
    ui->m_listview->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
}


void MainWindow::on_m_pbn_init_clicked()
{
    m_modellist->setStringList(m_strlist);
//    ui->m_listview->setModel(m_modellist); //已经做过关联了
}

void MainWindow::on_m_pbn_clean_clicked()
{
    m_modellist->removeRows(0, m_modellist->rowCount());
}

void MainWindow::on_m_pbn_additem_clicked()
{
    m_modellist->insertRow(m_modellist->rowCount());

    QModelIndex index = m_modellist->index(m_modellist->rowCount()-1, 0);//行列
    m_modellist->setData(index, "添加行", Qt::DisplayRole);

    ui->m_listview->setCurrentIndex(index);
}

void MainWindow::on_m_pbn_insertitem_clicked()
{
    QModelIndex index = ui->m_listview->currentIndex();

    m_modellist->insertRow(index.row());
    m_modellist->setData(index, "插入行", Qt::DisplayRole); //只显示文字
    ui->m_listview->setCurrentIndex(index);
}

void MainWindow::on_m_pbn_delItem_clicked()
{
    QModelIndex index = ui->m_listview->currentIndex();
    m_modellist->removeRow(index.row()); //通过操作model来处理view
}

//TODO 这里的插入位置不对
void MainWindow::on_m_pbn_moveup_clicked()
{
    int row_index = ui->m_listview->currentIndex().row();
    //这里插入的位置不对
    m_modellist->moveRow(QModelIndex(),row_index,QModelIndex(), row_index-1);
}

//TODO 虽然这里插入位置对了 但是需要研究moveRow
void MainWindow::on_m_pbn_movedown_clicked()
{
    int row_index = ui->m_listview->currentIndex().row();
    QModelIndex temp = QModelIndex();
    //父索引、源行号、目标父索引和目标位置
    m_modellist->moveRow(temp,row_index,temp, row_index+2);
}
#include <QDebug>
void MainWindow::on_m_pbn_sort_clicked(bool checked)
{
    qDebug()<<"bool:"<<checked;
    m_modellist->sort(0, checked?(Qt::AscendingOrder):(Qt::DescendingOrder));
}

//选中行是触发
void MainWindow::on_m_listview_clicked(const QModelIndex &index)
{
    QString str = QString::asprintf("模型索引:row=%d, column=%d;",index.row(),index.column());

    QVariant var = m_modellist->data(index, Qt::DisplayRole);
    QString str2= var.toString();
    ui->statusbar->showMessage(str+str2);
}

void MainWindow::on_m_checkBox_enableEdit_clicked(bool checked)
{
    ui->m_listview->setEditTriggers(checked?(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked):QAbstractItemView::NoEditTriggers);
}

void MainWindow::on_m_pbn_clean_display_clicked()
{
    ui->m_pte_display->clear();
}

void MainWindow::on_m_pbn_display_clicked()
{
    //直接获取QListModel上的QStringlist数据，显示在控件输出框中
    QStringList list = m_modellist->stringList();

    ui->m_pte_display->clear();
    for(int i=0; i<list.size(); i++)
        ui->m_pte_display->appendPlainText(list.at(i));
}
